package br.com.fafica.fast_restaurant.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JSeparator;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import br.com.fafica.fast_restaurant.control.ControlProduto;
import br.com.fafica.fast_restaurant.model.dao.FabricaConecxao;

public class TelaPesquisarProduto extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTable table;

			public void run() {
				try {
					TelaPesquisarProduto frame = new TelaPesquisarProduto();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

	public TelaPesquisarProduto() {
		setTitle("PESQUISA PRODUTO");
		final TelaPesquisarProduto estaTela = this;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(0, 100, 0));
		
		JLabel lblNomeProduto = new JLabel();
		lblNomeProduto.setText("NOME PRODUTO:");
		lblNomeProduto.setForeground(new Color(255, 215, 0));
		lblNomeProduto.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNomeProduto.setBackground(Color.GRAY);
		
		textField = new JTextField();
		textField.setColumns(10);
		
		JButton button_4 = new JButton("PESQUISAR");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
				ArrayList<String> Parametros = new ArrayList<String>();
				Parametros.add(textField.getText().toUpperCase());
				
				Vector<String> vColunas = new Vector<String>();
				vColunas = PegaColunaDaGrade();
				
				DefaultTableModel objTbela = new DefaultTableModel(vColunas, 0);
				
				ControlProduto cntrlProduto = new ControlProduto();
				
				objTbela = cntrlProduto.Pesquisa(Parametros, objTbela);
				
				
				table.setModel(objTbela);
				
			}
			
			
private Vector<String> PegaColunaDaGrade() {
				
				Vector<String> colunasTabela = new Vector<String>();
				colunasTabela.add("cod");
				colunasTabela.add("preco");
				colunasTabela.add("nome");
		
						
				return colunasTabela; }
				
				
			
		});
		button_4.setForeground(new Color(255, 215, 0));
		button_4.setBackground(new Color(0, 100, 0));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNomeProduto)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(textField, GroupLayout.PREFERRED_SIZE, 521, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(21, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(23)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNomeProduto, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_4))
					.addContainerGap(22, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		JSeparator separator = new JSeparator();
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(0, 100, 0));
		
		JLabel lblListaProduto = new JLabel();
		lblListaProduto.setText("LISTA PRODUTO:");
		lblListaProduto.setForeground(new Color(255, 215, 0));
		lblListaProduto.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblListaProduto.setBackground(Color.GRAY);
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(lblListaProduto, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 750, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(24, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblListaProduto, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 301, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(22, Short.MAX_VALUE))
		);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
			},
			new String[] {
				"C\u00D3DIGO", "PRE\u00C7O", "NOME"
			}
		));
		table.getColumnModel().getColumn(2).setPreferredWidth(123);
		scrollPane.setViewportView(table);
		panel_1.setLayout(gl_panel_1);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(new Color(255, 255, 255));
		
		JButton button_2 = new JButton("CANCELAR");
		button_2.setBackground(new Color(0, 100, 0));
		button_2.setForeground(new Color(255, 215, 0));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				estaTela.setVisible(false);
				TelaPesquisar telaPesquisar = new TelaPesquisar();
				telaPesquisar.setVisible(true);
			}
		});
		
		JButton button_3 = new JButton("SALVAR");
		button_3.setBackground(new Color(0, 100, 0));
		button_3.setForeground(new Color(255, 215, 0));
		
		JButton button = new JButton("EDITAR");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String cod = (String)table.getModel().getValueAt(table.getSelectedRow(),0);
				String preco = (String)table.getModel().getValueAt(table.getSelectedRow(),1);
				String nome = (String)table.getModel().getValueAt(table.getSelectedRow(),2);
			
				
				

				TelaEditarProduto jp = new TelaEditarProduto(null, true, cod, preco, nome);				
				jp.setVisible(true);
				
			}
		});
		button.setBackground(new Color(0, 100, 0));
		button.setForeground(new Color(255, 215, 0));
		
		JButton button_1 = new JButton("EXCLUIR");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try{
					Connection conn = FabricaConecxao.GeraConexao();	
					String query = "delete from cad_produto where cod = ?";
					PreparedStatement cmd = conn.prepareStatement(query);
					String index;
					index = (String) table.getModel().getValueAt(table.getSelectedRow(),0);
					
					
					
					cmd.setString(1, index);
					cmd.executeUpdate();
					
					}catch (SQLException ex) {
						System.out.println("Ocorreu um erro sql" + ex.getMessage());
					}
				
				ArrayList<String> Parametros = new ArrayList<String>();
				Parametros.add(textField.getText().toUpperCase());
				
				Vector<String> vColunas = new Vector<String>();
				vColunas = PegaColunaDaGrade();
				
				DefaultTableModel objTbela = new DefaultTableModel(vColunas, 0);
				
				ControlProduto cntrlProduto = new ControlProduto();
				
				objTbela = cntrlProduto.Pesquisa(Parametros, objTbela);
				
				
				table.setModel(objTbela);
				
			}
			
			
private Vector<String> PegaColunaDaGrade() {
				
				Vector<String> colunasTabela = new Vector<String>();
				colunasTabela.add("cod");
				colunasTabela.add("preco");
				colunasTabela.add("nome");
		
						
				return colunasTabela;
				
				
				
			}
		});
		button_1.setBackground(new Color(0, 100, 0));
		button_1.setForeground(new Color(255, 215, 0));
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap(240, Short.MAX_VALUE)
					.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
						.addComponent(button, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(16, Short.MAX_VALUE))
		);
		panel_2.setLayout(gl_panel_2);
		
		JSeparator separator_1 = new JSeparator();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 782, GroupLayout.PREFERRED_SIZE)
						.addComponent(separator, GroupLayout.PREFERRED_SIZE, 781, GroupLayout.PREFERRED_SIZE)
						.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, 783, GroupLayout.PREFERRED_SIZE)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 784, GroupLayout.PREFERRED_SIZE)
						.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 774, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(separator, GroupLayout.PREFERRED_SIZE, 5, GroupLayout.PREFERRED_SIZE)
					.addGap(13)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 360, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, 8, GroupLayout.PREFERRED_SIZE)
					.addGap(12)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
